/*
 	pulse.h

 	Copyright (c) 2008 Bojan Rajkovic

 	Permission is hereby granted, free of charge, to any person obtaining a copy
 	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 	copies of the Software, and to permit persons to whom the Software is
 	furnished to do so, subject to the following conditions:
	
 	The above copyright notice and this permission notice shall be included in
 	all copies or substantial portions of the Software.

 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 	THE SOFTWARE.
*/

#pragma once
#include "utility.h"
#include "sqlite_wrapper.h"
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pcrecpp.h>
#include <unistd.h>

#define PULSE_REQUEST "GET /pulse.php?username=%s&password=%s&keys=%li HTTP/1.1\r\nHost: %s\r\nUser-Agent: keycounter %s\r\n\r\n"
#define HTTP_STATUS_CODE_OK 200
#define HTTP_STATUS_CODE_MOVED_PERMANENTLY 301
#define HTTP_STATUS_CODE_FOUND 302
#define HTTP_STATUS_CODE_SEE_OTHER 303
#define HTTP_STATUS_CODE_MOVED_TEMPORARILY 307
#define HTTP_STATUS_CODE_BAD_REQUEST 400
#define HTTP_STATUS_CODE_UNATHORIZED 401
#define HTTP_STATUS_CODE_FORBIDDEN 402
#define HTTP_STATUS_CODE_NOT_FOUND 404 /* in essence, this and 410 are the same */
#define HTTP_STATUS_CODE_GONE 410
#define HTTP_STATUS_INTERNAL_ERROR 500
#define HTTP_STATUS_BAD_GATEWAY 502
#define HTTP_STATUS_SERVICE_UNAVAILABLE 503
#define HTTP_STATUS_VERSION_UNSUPPORTED 505
#define SQLITE_OK           0   /* Successful result */
#define SQLITE_ERROR        1   /* SQL error or missing database */
#define SQLITE_INTERNAL     2   /* An internal logic error in SQLite */
#define SQLITE_PERM         3   /* Access permission denied */
#define SQLITE_ABORT        4   /* Callback routine requested an abort */
#define SQLITE_BUSY         5   /* The database file is locked */
#define SQLITE_LOCKED       6   /* A table in the database is locked */
#define SQLITE_NOMEM        7   /* A malloc() failed */
#define SQLITE_READONLY     8   /* Attempt to write a readonly database */
#define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite_interrupt() */
#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT     11   /* The database disk image is malformed */
#define SQLITE_NOTFOUND    12   /* (Internal Only) Table or record not found */
#define SQLITE_FULL        13   /* Insertion failed because database is full */
#define SQLITE_CANTOPEN    14   /* Unable to open the database file */
#define SQLITE_PROTOCOL    15   /* Database lock protocol error */
#define SQLITE_EMPTY       16   /* (Internal Only) Database table is empty */
#define SQLITE_SCHEMA      17   /* The database schema changed */
#define SQLITE_TOOBIG      18   /* Too much data for one row of a table */
#define SQLITE_CONSTRAINT  19   /* Abort due to contraint violation */
#define SQLITE_MISMATCH    20   /* Data type mismatch */
#define SQLITE_MISUSE      21   /* Library used incorrectly */
#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
#define SQLITE_AUTH        23   /* Authorization denied */
#define SQLITE_ROW         100  /* sqlite_step() has another row ready */
#define SQLITE_DONE        101  /* sqlite_step() has finished executing */

using namespace std;
using namespace pcrecpp;

struct pulse_info {
	unsigned long keys; /* number of keys since last pulse */
	const char* user_name; /* unused for now */
	const char* password; /* unused for now */
};

class database_open_failed_exception : public exception {
  virtual const char* what() const throw()
  {
    return "Could not open database file.";
  }
};

class pulse {
	
	public:
		static unsigned int pulse_to_web(pulse_info pulse);
		static unsigned int pulse_to_db(pulse_info pulse);
		static void handle_http_error(int http_error);
		static void zero_pulse_file();
		static void save_pulse_file(unsigned long keys);

};
